<template>
	<view class="search">
		<!-- 搜索框 -->
		<input-search @search="onSearch" @onInput="onInput" ref="inputSearch" />
		<block v-if="isSearchShow">
			<!-- 历史搜索 -->
			<view class="search-records" v-if="recordsList.length">
				<view class="search-title search-records-title">
					<text>历史搜索</text>
					<icon class="icon-DETETE" @click="deleteRecords"></icon>
				</view>
				<view class="search-records-list">
					<view class="search-records-item" v-for="(item, index) in recordsList" :key="index" @click="onSearch(item)">{{ item }}</view>
				</view>
			</view>
			<!-- 指定内容 -->
			<view class="specify-content">
				<view class="search-title specify-content-title"><text>搜索指定内容</text></view>
				<view class="specify-content-list">
					<view class="specify-content-item" @click="bindFuncSearch(0)">
						<icon class="icon-WZ"></icon>
						<text>文章</text>
					</view>
					<view class="specify-content-item" @click="bindFuncSearch(1)">
						<icon class="icon-SP"></icon>
						<text>视频</text>
					</view>
					<view class="specify-content-item" @click="bindFuncSearch(2)">
						<icon class="icon-WD"></icon>
						<text>问答</text>
					</view>
				</view>
			</view>
			<!-- 热门搜索 -->
			<view class="search-hot">
				<view class="search-title search-hot-title">热门搜索</view>
				<view class="search-hot-list">
					<view class="search-hot-item">猫咪绝育</view>
					<view class="search-hot-item">猫咪感冒怎么办</view>
					<view class="search-hot-item">猫咪驱虫</view>
					<view class="search-hot-item">猫咪绝育</view>
					<view class="search-hot-item">猫咪感冒怎么办</view>
					<view class="search-hot-item">猫咪驱虫</view>
				</view>
			</view>
		</block>
		<view class="search-list" v-else>
			<!-- tab选项卡 -->
			<view class="popular-tabs">
				<v-tabs
					v-model="current"
					:tabs="tabs"
					:scroll="false"
					color="#333333"
					activeColor="#34D1A9"
					fontSize="32rpx"
					lineHeight="6rpx"
					lineColor="#34D1A9"
					lineRadius="22rpx"
					lineScale="0.3"
					@change="changeTab"
				></v-tabs>
			</view>
			<!-- 有数据 -->
			<search-list ref="searchList" :current="current" />
			<end-img />
			<!-- 缺省图 -->
			<!-- <view class="search-no-list">
				<image src="https://nabercat.oss-cn-hangzhou.aliyuncs.com/pet-hospital/search-queshengimg.png" mode=""></image>
				<view class="no-list-text">没有搜到哦，试试咨询医生？</view>
				<button class="no-list-btn" open-type="contact">咨询医生</button>
			</view> -->
		</view>
	</view>
</template>

<script>
import InputSearch from '@/components/inputSearch/inputSearch.vue';
import SearchList from '../components/searchList/searchList.vue';
import EndImg from '@/components/endImg/endImg.vue';
import vTabs from '@/components/v-tabs/v-tabs.vue';
export default {
	components: {
		InputSearch,
		SearchList,
		EndImg,
		vTabs
	},
	data() {
		return {
			current: 0,
			tabs: ['文章', '视频', '问答'],
			isSearchShow: true, //控制搜索记录显隐
			recordsList: [] //搜索记录
		};
	},
	methods: {
		// 输入框点击搜索
		onSearch(val) {
			this.isSearchShow = false;
			this.$refs.inputSearch.searchValue = val;
			// 存储搜索记录
			let InputArr = [...new Set([val, ...this.recordsList])];
			let InputSearchArr = InputArr.filter((item, index) => {
				return index < 8;
			});
			uni.setStorageSync('InputSearch', InputSearchArr);
			this.recordsList = uni.getStorageSync('InputSearch');
		},
		// 监听输入框
		onInput(val) {
			if (!val) {
				this.isSearchShow = true;
			}
		},
		// 清除搜索记录
		deleteRecords() {
			uni.removeStorageSync('InputSearch');
			this.recordsList = uni.getStorageSync('InputSearch');
		},
		// 选择类目跳转
		bindFuncSearch(event) {
			if (!this.$refs.inputSearch.searchValue) {
				uni.showToast({
					title: '请输入关键字',
					duration: 2000,
					icon: 'none'
				});
				return false;
			}
			this.current = event;
			this.isSearchShow = false;
		},
		// tab选项卡
		changeTab(event) {
			console.log(event);
		}
	},
	onLoad(options) {
		// 查询搜索记录
		this.recordsList = uni.getStorageSync('InputSearch') ? uni.getStorageSync('InputSearch') : [];
	}
};
</script>

<style scoped lang="scss">
.search {
	padding: 24rpx 32rpx;
	.search-records {
		margin: 24rpx 0 36rpx;
		.search-records-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			.icon-DETETE {
				width: 32rpx;
				height: 34rpx;
				background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/pet-hospital/search-icon-delete.png) no-repeat;
				background-size: cover;
			}
		}
		.search-records-list {
			display: flex;
			flex-wrap: wrap;
			.search-records-item {
				height: 36rpx;
				line-height: 36rpx;
				padding: 0 16rpx;
				background: #f7f7f7;
				border-radius: 20rpx;
				margin: 0 24rpx 24rpx 0;
				font-size: 24rpx;
				color: #333333;
			}
		}
	}
	.specify-content {
		margin: 24rpx 0 60rpx;
		.specify-content-list {
			display: flex;
			.specify-content-item {
				height: 60rpx;
				background: rgba(52, 209, 169, 0.12);
				border-radius: 34rpx;
				border: 2rpx solid #34d1a9;
				padding: 0 40rpx;
				font-size: 28rpx;
				font-weight: 600;
				color: #34d1a9;
				margin-right: 40rpx;
				display: flex;
				align-items: center;
				line-height: 60rpx;
				& > icon {
					width: 28rpx;
					height: 28rpx;
					margin-right: 2rpx;
				}
				.icon-WZ {
					background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/pet-hospital/icon-search-wenzhang.png) no-repeat;
					background-size: cover;
				}
				.icon-SP {
					background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/pet-hospital/icno-search-shiping.png) no-repeat;
					background-size: cover;
				}
				.icon-WD {
					background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/pet-hospital/icon-search-wenda.png) no-repeat;
					background-size: cover;
				}
			}
		}
	}
	.search-hot {
		.search-hot-title {
			padding-left: 48rpx;
			position: relative;
			&::after {
				content: '';
				width: 32rpx;
				height: 44rpx;
				background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/pet-hospital/icon-search-hot.png) no-repeat;
				background-size: cover;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
			}
		}
		.search-hot-list {
			display: flex;
			flex-wrap: wrap;
			.search-hot-item {
				height: 36rpx;
				line-height: 36rpx;
				padding: 0 16rpx;
				background: #f7f7f7;
				border-radius: 20rpx;
				margin: 0 24rpx 24rpx 0;
				font-size: 24rpx;
				color: #333333;
			}
		}
	}
	.search-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333333;
		line-height: 44rpx;
		margin-bottom: 24rpx;
	}
}
.search-list {
	margin-top: 10rpx;
	.popular-tabs {
		margin-bottom: 32rpx;
	}
	.search-no-list {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 200rpx;
		& > image {
			width: 210rpx;
			height: 180rpx;
			margin-bottom: 4rpx;
		}
		.no-list-text {
			font-size: 28rpx;
			color: #999999;
			line-height: 40rpx;
			margin-bottom: 40rpx;
		}
		.no-list-btn {
			width: 370rpx;
			height: 80rpx;
			background: #34d1a9;
			border-radius: 40rpx;
			font-size: 32rpx;
			color: #f5faff;
			line-height: 80rpx;
		}
	}
}
</style>
